<?php

function printPageNav($maxPage, $link, $curr) {
    if ($maxPage>1) {
		// print the link to access each page
                $width = 7;

		$nav = '';
                $pagestart = $curr-$width;
                if ($pagestart<1)
                    $pagestart = 1;
                $pageend = $curr+$width;
                if ($pageend>$maxPage)
                    $pageend = $maxPage;
                
		for($page = $pagestart; $page <= $pageend; $page++)
		{
			if ($page == $curr)
			{
				$nav .= " $page ";   // no need to create a link to current page
			}
			else
			{
				$nav .= " <a href=\"{$link}page=$page\">$page</a> ";
			}
		}

		// creating previous and next link
		// plus the link to go straight to
		// the first and last page

		if ($curr > 1)
		{
			$page = $curr - 1;
			$prev = " <a href=\"{$link}page=$page\">[Prev]</a> ";

			
		}
		else
		{
			$prev  = '&nbsp;'; // we're on page one, don't print previous link
			
		}

                if($curr>$width+1) {
                    $first = " <a href=\"{$link}page=1\">1</a> ";
                    if ($curr>$width+2)
                        $presec = " <a href=\"{$link}page=".($pagestart-1)."\">...</a> ";
                } else {
                    $first = '&nbsp;'; // nor the first page link
                }

		if ($curr < $maxPage)
		{
			$page = $curr + 1;
			$next = " <a href=\"{$link}page=$page\">[Next]</a> ";

		}
		else
		{
			$next = '&nbsp;'; // we're on the last page, don't print next link
			$last = '&nbsp;'; // nor the last page link
		}

                if ($curr+$width<$maxPage) {
			$last = " <a href=\"{$link}page=$maxPage\">$maxPage</a> ";
                        if ($curr+$width+1<$maxPage)
                            $nextsec = " <a href=\"{$link}page=".($pageend+1)."\">...</a> ";
                } else {
			$last = '&nbsp;'; // nor the last page link
                }

		// print the navigation link
		echo $prev . $first . $presec. $nav . $nextsec. $last . $next;
    } // end if ($maxPage>1)
}

?>
